define(function(require, exports, module) {

	"use strict";
	require("mui");
	require("jquery");
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var template = require("template");

	var muiTips = require("muiTips"); //封装的弹框
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	var dingTalkNav = require('dd_config');
	dingTalkNav.RightNavBar(false, "更多", "default"); //钉钉右侧按钮隐藏
	document.title = "";

	function getRightFun() {
		deleteGoods();
	}
	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityGoods = authorityInit.getJurisdiction(authorityData, sourceType.INFO_GOODS); //货品权限判断
	if(authorityGoods.indexOf(3) === -1) {
		muiTips.alertTips(' ', '您没有操作货品的权限，请联系管理员开通吧！', 2, 3, "default");
		//钉钉右边按钮操作
		dingTalkNav.RightNavBar(false, "删除", "default");
	} else {
		if(sessionStorage.flags) {
			$(".mui-title").html("");
			$("#sub-btn").show();
			$("#sub-btn .changeBtn").show();
			$("#deleteBtn").show();
			//钉钉右边按钮操作
			dingTalkNav.RightNavBar(true, "删除", getRightFun);
		}

		mui("body").on("tap", "#deleteBtn", function() { //点击删除  删除货品
			deleteGoods();
		});

		mui("body").on("tap", ".changeBtn", function() {
			sessionStorage.setItem("productUpdate", "productUpdate");
			document.location.href = "./changeProduct.html?dd_nav_bgcolor=FFFF943E";
		});
	}

	$(function() {
		if(sessionStorage.flags) {
			$(".mui-title").html("货品详情");
		} else {
			$(".mui-title").html("库存查询");
		}
		dingTalkNav.setTitleText($(".mui-title").html());
	});

	mui('body').on('tap', 'a', function() {
		document.location.href = this.href + "?dd_nav_bgcolor=FFFF943E";
	});

	mui.init({
		swipeBack: true //启用右滑关闭功能
	});

	var stockQuery = sessionStorage.getItem('data-stockQuery');

	var goodsInfo = JSON.parse(stockQuery);
	var option = {};
	var dataModel = {
		"id": goodsInfo.id,
		"serialNo": goodsInfo.goodsSerial

	};
	console.log(goodsInfo);
	abp.services.app.whInventoryTotal.getGoodsWhInvneotory(dataModel, option).
	done(function(data) {

		data.goodsInfo = goodsInfo;

		var html = template("bindView", data);

		$("#view").html(html);

	}).fail(function(data) {

	});

	mui('body').on('tap', '.mui-popover-action li>a', function() {
		var a = this,
			parent;
		//根据点击按钮，反推当前是哪个actionsheet
		for(parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
			if(parent.classList.contains('mui-popover-action')) {
				break;
			}
		}
		//关闭actionsheet
		mui('#' + parent.id).popover('toggle');
		//			if($(this).text()=='编辑'){
		//				mui.alert('<ul class="mui-row pop-tabmain"><li class="mui-col-xs-4 mui-col-sm-4"style="text-align: center;"><a class="yuanico"><span>!</span></a></li><li class="mui-col-xs-8 mui-col-sm-8" style="text-align: left;">您没有重置草稿的权限,请通知管理员进行设置</li></ul>', ' ', function() {
		//				},'div');
		//			};
	});

	mui("body").on("tap", "#back", function() {
		clearSession();
	});
	//返回清缓存

	//钉钉返回
	//	var dingTalkNav = require('dd_config');
	if(sessionStorage.getItem("flags")) {
		dingTalkNav.LeftNavBar(clearSessionDD);
		//ad
		dingTalkNav.leftAndroid(clearSessionDD);
	} else {
		dingTalkNav.LeftNavBar(clearSessionDDStockQuery);
		//ad
		dingTalkNav.leftAndroid(clearSessionDDStockQuery);
	}

	function deleteGoods() {
		var serialNo = $(".goodsSerial").text();
		var dataModelDEL = {
			"id": goodsInfo.id,
			"serialNo": serialNo
		};
		var btnArray = ['否', '是'];
		mui.confirm('你确定要删除吗？', '提示', btnArray, function(e) {
			if(e.index == 1) {
				abp.services.app.infoGoods.delete(dataModelDEL, option)
					.done(function(data) {
						clearSession();
						sessionStorage.removeItem("data-stockQuery");
						mui.back();
					})
					.fail(function(data) {
						var muiTips = require("muiTips");
						muiTips.alertTips(' ', data.message, 2, 3, "default");
					});
			} else {

			}
		});
	}

	function clearSession() {
		sessionStorage.removeItem("data-stockQuery");
		sessionStorage.removeItem("flags");
		sessionStorage.removeItem("goodsId");
		sessionStorage.removeItem("goodsName");
		sessionStorage.removeItem("goodsSerial");
		sessionStorage.removeItem("goodsStock");
		sessionStorage.removeItem("goodsUnit");
	}

	function clearSessionDD() {
		sessionStorage.removeItem("data-stockQuery");
		sessionStorage.removeItem("flags");
		sessionStorage.removeItem("goodsId");
		sessionStorage.removeItem("goodsName");
		sessionStorage.removeItem("goodsSerial");
		sessionStorage.removeItem("goodsStock");
		sessionStorage.removeItem("goodsUnit");
		document.location.href = "../../src/chioceProduct/chioceProductList.html?dd_nav_bgcolor=FFFF943E";
	}
	//stockQuery
	function clearSessionDDStockQuery() {
		sessionStorage.removeItem("data-stockQuery");
		sessionStorage.removeItem("flags");
		sessionStorage.removeItem("goodsId");
		sessionStorage.removeItem("goodsName");
		sessionStorage.removeItem("goodsSerial");
		sessionStorage.removeItem("goodsStock");
		sessionStorage.removeItem("goodsUnit");
		document.location.href = "../../src/stock/stockQuery.html?dd_nav_bgcolor=FFFF943E";
	}

});